package zhang.automobileservice;

import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.AdapterView;
import android.widget.ImageView;
import android.widget.ListView;

import com.android.volley.Response;
import com.android.volley.VolleyError;
import com.handmark.pulltorefresh.library.PullToRefreshBase;
import com.handmark.pulltorefresh.library.PullToRefreshListView;

import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;

import java.util.ArrayList;
import java.util.List;

import adapter.NewsAdapter;
import bean.NewsBean;
import utils.RequestQueueUtils;

public class NewsConsultActivity extends FarterActivity implements View.OnClickListener {

    private PullToRefreshListView lv_newslist;
    private ArrayList<NewsBean> newList;
    private NewsAdapter newsAdapter;
    private ImageView finish;
    private int page=1;


    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_news_consult);
        lv_newslist = (PullToRefreshListView) findViewById(R.id.lv_newslist);
        finish = (ImageView) findViewById(R.id.finish);
        finish.setOnClickListener(this);
        newList = new ArrayList<>();
        bindView();
        initData();
    }

    private void initData() {
        String url = "http://api.dagoogle.cn/news/get-news?tableNum=4&page="+page;
        if(page==1){
            newList.clear();
        }
        RequestQueueUtils.exectueJsonRequest(this, url, new Response.Listener<JSONObject>() {
            @Override
            public void onResponse(JSONObject jsonObject) {
                try {
                    if ("200".equals(jsonObject.getString("status"))) {
                        JSONArray array = jsonObject.getJSONArray("data");
                        bindList(array);
                    }
                } catch (JSONException e) {
                    e.printStackTrace();
                }
                newsAdapter.notifyDataSetChanged();
                lv_newslist.onRefreshComplete();
                lv_newslist.setMode(PullToRefreshBase.Mode.BOTH);
            }
        }, new Response.ErrorListener() {
            @Override
            public void onErrorResponse(VolleyError volleyError) {

            }
        });
        lv_newslist.setOnRefreshListener(new PullToRefreshBase.OnRefreshListener2<ListView>() {
            @Override
            public void onPullDownToRefresh(PullToRefreshBase<ListView> pullToRefreshBase) {
                page=1;
                initData();
            }

            @Override
            public void onPullUpToRefresh(PullToRefreshBase<ListView> pullToRefreshBase) {
                page++;
                initData();
            }
        });
    }

    private List<NewsBean> bindList(JSONArray object) {
        try {
            for (int i = 0; i < object.length(); i++) {
                NewsBean news = new NewsBean();
                JSONObject o = (JSONObject) object.get(i);
                news.setNews_id(o.getString("news_id"));
                news.setTop_image(o.getString("top_image"));
                news.setReply_count(o.getString("reply_count"));
                news.setEdit_time(o.getString("edit_time"));
                news.setTitle(o.getString("title"));
                news.setContent(o.getString("content"));
                news.setSource(o.getString("source"));
                newList.add(news);
            }
        } catch (JSONException e) {
            e.printStackTrace();
        }
        return newList;

    }

    private void bindView() {
        newsAdapter = new NewsAdapter(newList, getApplicationContext());
        lv_newslist.setAdapter(newsAdapter);
    }

    @Override
    protected void onResume() {
        super.onResume();
        lv_newslist.setOnItemClickListener(new AdapterView.OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
                Intent intent = new Intent(getApplicationContext(), NewsDetialActivity.class);
                Bundle bundle = new Bundle();
                bundle.putSerializable("NewsBean", newList.get(position));
                intent.putExtras(bundle);
                startActivity(intent);
            }
        });
    }

    @Override
    public void onClick(View v) {
        switch (v.getId()) {
            case R.id.finish:
                finish();
                break;
        }
    }

}
